<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="10">
        <!--饼状图1 -->
        <div id="echarts1" :style="{height:height,width:width}" />
      </el-col>
      <el-col :span="10">
        <!--饼状图2 -->
        <div id="echarts2" :style="{height:height,width:width}" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getGoodTypeInfo } from "@/api/dashboard";
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  name: "GoodTypeInfo",
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },

  data() {
    return {
      chart: null,
      chart1: null,
      goodTypeName: [],
      goodTypeCount: [],
      goodTypePay: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(goodTypeName, goodTypeCount) {
      this.chart = echarts.init(
        document.getElementById("echarts1"),
        "macarons"
      );

      this.chart.setOption({
        title: { text: "商品分类情况" },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "10",
          data: this.goodTypeName,
        },
        series: [
          {
            name: "商品分类分布",
            type: "pie",
            roseType: "radius",
            radius: [15, 95],
            center: ["50%", "38%"],
            data: goodTypeCount,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
          },
        ],
      });
    },
    initChart1(goodName, goodPayCount) {
      this.chart1 = echarts.init(
        document.getElementById("echarts2"),
        "macarons"
      );

      this.chart1.setOption({
        title: { text: "商品分类销售额" },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "10",
          data: goodName,
        },
        series: [
          {
            name: "商品分类售卖情况分布",
            type: "pie",
            roseType: "radius",
            radius: [15, 95],
            center: ["50%", "38%"],
            data: goodPayCount,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
          },
        ],
      });
    },
  },

  //初始化方法
  created() {
    // 获取请求数据
    getGoodTypeInfo()
      .then((result) => {
        // 遍历数据
        for (var item of result.data) {
          this.goodTypeName.push(item.goodTypeName);
          this.goodTypeCount.push({
            value: item.count,
            name: item.goodTypeName,
          });
          this.goodTypePay.push({
            value: item.payCount,
            name: item.goodTypeName,
          });
        }
        //初始化扇形图1 扇形图2
        this.initChart(this.goodTypeName, this.goodTypeCount);
        this.initChart1(this.goodTypeName, this.goodTypePay);
      })
      .catch((err) => {});
  },
};
</script>
